package equipmentBusiness;

import tools.DateDeviation;
import tools.MyString;

import java.util.HashMap;
import java.util.Objects;

/**
 * Created by yangxianming on 2018/1/11.
 * 创建异常数据
 */
public class CreateTestFile {
    String path;
    EquipmentExcelModel em;
    DataGenerator dg;
    int countLines = 5;                         //默认一共几行
    String communityName = "浦东一号";     //默认小区
    //errData的key值说明： 左边表示列号， 右边表示行号，起始值0。第一行的数据行下标0，也就是不算字段
    //errData的key以x-y的形式标记出表格中需要定位的数据，以便在正确的位置替换掉合法的数据
    HashMap<String, String> changeData = new HashMap<String, String>();

    CreateTestFile(String path){
        this.path = path;
        this.em = new EquipmentExcelModel();
        this.dg = new DataGenerator();
    }

    public boolean test(String fileName) {
        //type[0]代表用例类型，type[1]代表当前类型的编号 type[2]代表最终文件编号
        String[] type = (fileName.split("\\."))[0].split("_");
        em.fileName = fileName;                 //文件名
        dg.isMaintenance = true;
        dg.isPatrol = true;


        if (Objects.equals(type[0], "0")){
            //设置合法的维保和巡检的间隔，
            if (Objects.equals(type[1], "2")){
                changeData.put("12-0","每周一次");
                changeData.put("12-1","每月一次");
                changeData.put("12-2","每季度一次");
                changeData.put("12-3","每半年一次");
                changeData.put("12-4","每年一次");
                changeData.put("19-0","每日一次");
                changeData.put("19-1","每周一次");
                changeData.put("19-2","每月一次");
                changeData.put("19-3","每季度一次");
            }
            //这里最大上传数量
            if (Objects.equals(type[1], "3"))
                countLines = 500;
        }

        if (Objects.equals(type[0], "1")){
            em.equipmentInstallSpace.set(0, "错误的字段名");
//            changeData.put("3-0","赫尔辛根默斯肯");
        }

        if (Objects.equals(type[0], "2")){
            changeData.put("0-1", "赫尔辛根默斯肯");
            changeData.put("0-2", "赫尔辛根默斯肯");
        }

        if (Objects.equals(type[0], "3")){
            //小区名
            if (Objects.equals(type[1], "1"))
                changeData.put("0-0","");
            //设备名称
            if (Objects.equals(type[1], "2"))
                changeData.put("1-4","");
            //设备编号
            if (Objects.equals(type[1], "3"))
                changeData.put("2-0","");
            //安装位置
            if (Objects.equals(type[1], "4"))
                changeData.put("3-4","");
            //设备状态
            if (Objects.equals(type[1], "5"))
                changeData.put("4-0","");
            //二级分类
            if (Objects.equals(type[1], "6"))
                changeData.put("5-4","");
            //是否安装设备房
            if (Objects.equals(type[1], "7"))
                changeData.put("6-0","");
            //所属设备房
            if (Objects.equals(type[1], "8"))
                changeData.put("7-4","");
            //是否维保
            if (Objects.equals(type[1], "9"))
                changeData.put("8-0","");
            //维保合同开始时间
            if (Objects.equals(type[1], "10"))
                changeData.put("9-4","");
            //维保合同结束时间
            if (Objects.equals(type[1], "11"))
                changeData.put("10-0","");
            //首次维保日期
            if (Objects.equals(type[1], "12"))
                changeData.put("11-4","");
            //维保周期
            if (Objects.equals(type[1], "13"))
                changeData.put("12-0","");
            //是否巡检
            if (Objects.equals(type[1], "14"))
                changeData.put("17-4","");
            //首次巡检日期
            if (Objects.equals(type[1], "15"))
                changeData.put("18-0","");
            //巡检周期
            if (Objects.equals(type[1], "16"))
                changeData.put("19-4","");
        }

        if (Objects.equals(type[0], "4")){
            //一次导入时的两个相同设备号
            if (Objects.equals(type[1], "1")){
                changeData.put("2-0", "sameNumber1985");
                changeData.put("2-4", "sameNumber1985");
            }
            if (Objects.equals(type[1], "2")){
                changeData.put("2-0", "sameWithMysql1985");
            }
        }

        if (Objects.equals(type[0], "5")){
            //设备状态
            if (Objects.equals(type[1], "1"))
                changeData.put("4-0","正在用");
            //二级分类风格是
            if (Objects.equals(type[1], "2"))
                changeData.put("5-4","量子");
            //是否安装设备房
            if (Objects.equals(type[1], "3"))
                changeData.put("6-0","不装");
            //所属设备房
            if (Objects.equals(type[1], "4"))
                changeData.put("7-4","不存在的设备房");
            //是否维保
            if (Objects.equals(type[1], "5"))
                changeData.put("8-0","不维保");
            //合同开始时间
            if (Objects.equals(type[1], "6"))
                changeData.put("9-4", DateDeviation.deviation("current,month,-2,yyyy年MM月dd日"));
            //合同结束时间
            if (Objects.equals(type[1], "7"))
                changeData.put("10-0",  DateDeviation.deviation("current,month,-6,yyyy.MM.dd"));
            //首次维保格式
            if (Objects.equals(type[1], "8"))
                changeData.put("11-4",  DateDeviation.deviation("current,date,+2,yyyy年MM月dd日"));
            //首次维保日期
            if (Objects.equals(type[1], "9"))
                changeData.put("11-0",  DateDeviation.deviation("current,month,0,yyyy-MM-") + "28");
            //维保周期
            if (Objects.equals(type[1], "10"))
                changeData.put("12-4",  "every Week");
            //是否巡检
            if (Objects.equals(type[1], "11"))
                changeData.put("17-0",  "不了");
            //首次巡检格式
            if (Objects.equals(type[1], "12"))
                changeData.put("18-4",  DateDeviation.deviation("current,date,+2,yyyy年MM月dd日"));
            //首次巡检日期
            if (Objects.equals(type[1], "13"))
                changeData.put("18-0",  DateDeviation.deviation("current,month,0,yyyy-MM-") + "28");
            //巡检周期格式异常
            if (Objects.equals(type[1], "14"))
                changeData.put("19-4",  "every month");
            //年间日期格式
            if (Objects.equals(type[1], "15"))
                changeData.put("22-0",  "11-12");
            //质保日期格式
            if (Objects.equals(type[1], "16"))
                changeData.put("23-4",  DateDeviation.deviation("current,year,+2,yyyy年MM月dd日"));
        }
        if (Objects.equals(type[0], "6")){
            //设备名称
            if (Objects.equals(type[1], "1"))
                changeData.put("1-0", MyString.copy("字", 21));
            //设备编号
            if (Objects.equals(type[1], "2"))
                changeData.put("2-4", MyString.copy("x", 21));
            //安装位置
            if (Objects.equals(type[1], "3"))
                changeData.put("3-0", MyString.copy("字", 31));
            //维保公司名字
            if (Objects.equals(type[1], "4"))
                changeData.put("13-4", MyString.copy("字", 31));
            //维保联系人
            if (Objects.equals(type[1], "5"))
                changeData.put("14-0", MyString.copy("字", 9));
            //维保内容
            if (Objects.equals(type[1], "6"))
                changeData.put("16-4", MyString.copy("字", 201));
            //责任人
            if (Objects.equals(type[1], "7"))
                changeData.put("20-0", MyString.copy("字", 9));
            //备注
            if (Objects.equals(type[1], "8"))
                changeData.put("24-4", MyString.copy("字", 201));
        }
        if (Objects.equals(type[0], "7")){
            //保合同结束时间必须晚于维保合同开始时间
            if (Objects.equals(type[1], "1")){
                changeData.put("9-0", DateDeviation.deviation("current,month,+2,yyyy-MM-dd"));
                changeData.put("10-0", DateDeviation.deviation("current,month,-2,yyyy-MM-dd"));
            }
            //首次维保日期必须晚于维保合同开始时间
            if (Objects.equals(type[1], "2"))
                changeData.put("11-4", DateDeviation.deviation("current,month,-2,yyyy-MM-dd"));
            //首次维保日期必须早于维保合同结束时间
            if (Objects.equals(type[1], "3"))
                changeData.put("11-0", DateDeviation.deviation("current,month,+4,yyyy-MM-dd"));
        }
        if (Objects.equals(type[0], "8")){
            this.countLines = 501;
        }



        return testType();
    }


    private boolean testType() {
        //这里是装在默认数据
        for (int i = 0; i < countLines; i++) {
            int c1 = 0; //列下标
            if (changeData.containsKey(String.valueOf(c1) + "-" + String.valueOf(i))){
                em.community.add(changeData.get(String.valueOf(c1) + "-" + String.valueOf(i)));
            }else {
                em.community.add(communityName);                                            //小区名
            }
            c1++;

            if (changeData.containsKey(String.valueOf(c1) + "-" + String.valueOf(i))){
                em.equipmentName.add(changeData.get(String.valueOf(c1) + "-" + String.valueOf(i)));
            }else {
                em.equipmentName.add(dg.getEquipmentName());                                //设备名称
            }
            c1++;

            if (changeData.containsKey(String.valueOf(c1) + "-" + String.valueOf(i))){
                em.equipmentNumber.add(changeData.get(String.valueOf(c1) + "-" + String.valueOf(i)));
            }else {
                em.equipmentNumber.add(dg.getEquipmentNumber());                            //设备编号
            }
            c1++;

            if (changeData.containsKey(String.valueOf(c1) + "-" + String.valueOf(i))){
                em.equipmentInstallSpace.add(changeData.get(String.valueOf(c1) + "-" + String.valueOf(i)));
            }else {
                em.equipmentInstallSpace.add(dg.getEquipmentInstallSpace());                //安装位置
            }
            c1++;

            if (changeData.containsKey(String.valueOf(c1) + "-" + String.valueOf(i))){
                em.equipmentStatus.add(changeData.get(String.valueOf(c1) + "-" + String.valueOf(i)));
            }else {
                em.equipmentStatus.add("在用");                                             //设备状态 在用 停用 已报废
            }
            c1++;

            if (changeData.containsKey(String.valueOf(c1) + "-" + String.valueOf(i))){
                em.equipmentTwoStage.add(changeData.get(String.valueOf(c1) + "-" + String.valueOf(i)));
            }else {
                em.equipmentTwoStage.add(dg.getEquipmentClassify(2));                       //设备二级分类
            }
            c1++;

            if (changeData.containsKey(String.valueOf(c1) + "-" + String.valueOf(i))){
                em.equipmentIsInstallRoom.add(changeData.get(String.valueOf(c1) + "-" + String.valueOf(i)));
            }else {
                em.equipmentIsInstallRoom.add(dg.getEquipmentIsInstallRoom(true)[0]);       //是否安装在设备房
            }
            c1++;

            if (changeData.containsKey(String.valueOf(c1) + "-" + String.valueOf(i))){
                em.equipmentInstallRoomName.add(changeData.get(String.valueOf(c1) + "-" + String.valueOf(i)));
            }else {
                em.equipmentInstallRoomName.add(dg.getEquipmentIsInstallRoom(true)[1]);     //所属设备房
            }
            c1++;

            if (changeData.containsKey(String.valueOf(c1) + "-" + String.valueOf(i))){
                em.isMaintenance.add(changeData.get(String.valueOf(c1) + "-" + String.valueOf(i)));
            }else {
                em.isMaintenance.add(dg.getIsMaintenance());                                //是否维保
            }
            c1++;

            if (changeData.containsKey(String.valueOf(c1) + "-" + String.valueOf(i))){
                em.maintenanceContractStart.add(changeData.get(String.valueOf(c1) + "-" + String.valueOf(i)));
            }else {
                em.maintenanceContractStart.add(dg.getMaintenanceContractStart_End("-1"));  //维保合同开始时间
            }
            c1++;

            if (changeData.containsKey(String.valueOf(c1) + "-" + String.valueOf(i))){
                em.maintenanceContractEnd.add(changeData.get(String.valueOf(c1) + "-" + String.valueOf(i)));
            }else {
                em.maintenanceContractEnd.add(dg.getMaintenanceContractStart_End("+3"));    //维保合同结束时间
            }
            c1++;

            if (changeData.containsKey(String.valueOf(c1) + "-" + String.valueOf(i))){
                em.maintenanceFirstTime.add(changeData.get(String.valueOf(c1) + "-" + String.valueOf(i)));
            }else {
                em.maintenanceFirstTime.add(dg.getMaintenanceFirstTime("+2"));              //首次维保日期
            }
            c1++;

            if (changeData.containsKey(String.valueOf(c1) + "-" + String.valueOf(i))){
                em.maintenanceCycles.add(changeData.get(String.valueOf(c1) + "-" + String.valueOf(i)));
            }else {
                em.maintenanceCycles.add(dg.getMaintenanceCycles());                        //维保周期
            }
            c1++;

            if (changeData.containsKey(String.valueOf(c1) + "-" + String.valueOf(i))){
                em.maintenanceCompany.add(changeData.get(String.valueOf(c1) + "-" + String.valueOf(i)));
            }else {
                em.maintenanceCompany.add(dg.getMaintenanceCompany());                      //维保公司
            }
            c1++;

            if (changeData.containsKey(String.valueOf(c1) + "-" + String.valueOf(i))){
                em.maintenanceContact.add(changeData.get(String.valueOf(c1) + "-" + String.valueOf(i)));
            }else {
                em.maintenanceContact.add(dg.getMaintenanceContact());                        //维保公司联系人
            }
            c1++;

            if (changeData.containsKey(String.valueOf(c1) + "-" + String.valueOf(i))){
                em.maintenancePhone.add(changeData.get(String.valueOf(c1) + "-" + String.valueOf(i)));
            }else {
                em.maintenancePhone.add(dg.getMaintenancePhone());                          //维保公司联系人电话
            }
            c1++;

            if (changeData.containsKey(String.valueOf(c1) + "-" + String.valueOf(i))){
                em.maintenanceContent.add(changeData.get(String.valueOf(c1) + "-" + String.valueOf(i)));
            }else {
                em.maintenanceContent.add(dg.getMaintenanceContent());                        //维保内容
            }
            c1++;

            if (changeData.containsKey(String.valueOf(c1) + "-" + String.valueOf(i))){
                em.isPatrol.add(changeData.get(String.valueOf(c1) + "-" + String.valueOf(i)));
            }else {
                em.isPatrol.add(dg.getIsPatrol());                                          //是否巡检
            }
            c1++;

            if (changeData.containsKey(String.valueOf(c1) + "-" + String.valueOf(i))){
                em.patrolFirstTime.add(changeData.get(String.valueOf(c1) + "-" + String.valueOf(i)));
            }else {
                em.patrolFirstTime.add(dg.getPatrolFirstTime("+3"));                        //首次巡检日期
            }
            c1++;

            if (changeData.containsKey(String.valueOf(c1) + "-" + String.valueOf(i))){
                em.patrolCycles.add(changeData.get(String.valueOf(c1) + "-" + String.valueOf(i)));
            }else {
                em.patrolCycles.add(dg.getPatrolCycles());                                  //巡检周期
            }
            c1++;

            if (changeData.containsKey(String.valueOf(c1) + "-" + String.valueOf(i))){
                em.equipmentContact.add(changeData.get(String.valueOf(c1) + "-" + String.valueOf(i)));
            }else {
                em.equipmentContact.add(dg.getEquipmentContact());                          //负责人
            }
            c1++;

            if (changeData.containsKey(String.valueOf(c1) + "-" + String.valueOf(i))){
                em.equipmentPhone.add(changeData.get(String.valueOf(c1) + "-" + String.valueOf(i)));
            }else {
                em.equipmentPhone.add(dg.getEquipmentPhone());                              //负责人手机
            }
            c1++;

            if (changeData.containsKey(String.valueOf(c1) + "-" + String.valueOf(i))){
                em.equipmentAnnualInspactionDate.add(changeData.get(String.valueOf(c1) + "-" + String.valueOf(i)));
            }else {
                em.equipmentAnnualInspactionDate.add(dg.getEquipmentAnnualInspactionDate());//年检日期
            }
            c1++;

            if (changeData.containsKey(String.valueOf(c1) + "-" + String.valueOf(i))){
                em.equipmentShelfLifeEndDate.add(changeData.get(String.valueOf(c1) + "-" + String.valueOf(i)));
            }else {
                em.equipmentShelfLifeEndDate.add(dg.getEquipmentShelfLifeEndDate("+2"));      //保日期
            }
            c1++;

            if (changeData.containsKey(String.valueOf(c1) + "-" + String.valueOf(i))){
                em.equipmentMark.add(changeData.get(String.valueOf(c1) + "-" + String.valueOf(i)));
            }else {
                em.equipmentMark.add(dg.getEquipmentMark());                                //备注
            }
        }

        return em.saveExcel(path);
    }
}
